<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Http\Controllers\SmsController;
use App\Models\CompanyUser;
use App\Models\PersonalUser;
use App\Models\User;
use App\Models\WeixinInfo;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use Tymon\JWTAuth\Facades\JWTAuth;
use Illuminate\Support\Facades\Hash;

class LoginController extends Controller
{
    //登录
    public function login(Request $request)
    {
        $username = trim($request->username);
        if (!$username) {
            return response()->json(['status' => 0, 'msg' => '账号/手机号码缺失！']);
        }
        $userInfo = User::where('mobile', $username)->first();

        if (!$userInfo) {
            return response()->json(['status' => 0, 'msg' => '该账户不存在！']);
        }

        if ($userInfo->status == -1) {
            return response()->json(['status' => 0, 'msg' => '该账户已注销！']);
        }

        $type = $request->type;
        switch ($type) {
            case 1:
                $password = trim($request->password);
                if (!$password) {
                    return response()->json(['status' => 0, 'msg' => '密码缺失！']);
                }
                if (strlen($password) < 6) {
                    return response()->json(['status' => 0, 'msg' => '密码至少六位数！']);
                }
                if (!Hash::check($password, $userInfo->password)) {
                    return response()->json(['status' => 0, 'msg' => '密码错误！']);
                }
                break;
            case 2:
                $code = trim($request->code);
                if (!$code) {
                    return response()->json(['status' => 0, 'msg' => '短信验证码缺失！']);
                }
                $sms = new SmsController();
                $res = $sms->checkCode($username, $code);
                if ($res['status'] == 0) {
                    return response()->json(['status' => 0, 'msg' => $res['msg']]);
                }
                break;
            default:
                return response()->json(['status' => 0, 'msg' => '非法操作！']);
                break;
        }

        $token = JWTAuth::fromUser($userInfo);
        if (!$token) {
            return response()->json(['status' => 0, 'msg' => '登录失败，请重试！']);
        }
        return response()->json(['status' => 1, 'token' => $token]);

    }

    //获取用户信息
    public function home()
    {
        $user = JWTAuth::parseToken()->touser();//获取用户信息
        return $user;

    }

    //退出
    public function logout()
    {
        JWTAuth::parseToken()->invalidate();//退出
        return '退出成功';
    }

    /**
     * 微信登录
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function wechatLogin(Request $request)
    {
        $unionId = $request->unionId;
        if (!$unionId) {
            return response()->json(['status' => 0, 'msg' => '关键参数缺失！']);
        }

        $wxInfo = DB::table('weixin_info')->where('unionid', $unionId)->first();
        if (!$wxInfo) {
            return response()->json(['status' => 0, 'msg' => '该微信号不存在！']);
        }

        $type = $request->type;
        $openIdField = 'openid' . $type;

        if (!$wxInfo->$openIdField && $request->openId) {
            //补全小程序信息
            DB::table('weixin_info')->where('id', $wxInfo->id)->update([$openIdField => $request->openId]);
        }

        $cate = $request->cate;
        switch ($cate) {
            case 1:
                $user = PersonalUser::where('wechatid', $wxInfo->id)->first();
                break;
            case 2:
                $user = CompanyUser::where('wechatid', $wxInfo->id)->first();
                break;
        }

        if (!$user) {
            return response()->json(['status' => 0, 'msg' => '该微信号未绑定！']);
        }

        $member = User::where('uid', $user->uid)->where('type', $cate)->first();
        if (!$member) {
            return response()->json(['status' => 0, 'msg' => '数据表users数据缺失！']);
        }

        $token = JWTAuth::fromUser($member);
        if (!$token) {
            return response()->json(['status' => 0, 'msg' => '登录失败，请重试！']);
        }
        return response()->json(['status' => 1, 'token' => $token]);

    }
}
